package ee.ut.math.chess.base.model.chesspiece;

import java.util.ArrayList;
import java.util.List;

import ee.ut.math.chess.base.model.Move;
import ee.ut.math.chess.base.model.SpecialMoveType;
import ee.ut.math.chess.game.model.Game;

public class Pawn extends ChessPiece {

	private static final long serialVersionUID = 1L;
	private boolean inInitialPosition = true;
	private long value = 1;

	public Pawn(Color color) {
		this.color = color;
	}

	@Override
	public void setInInitialPosition(boolean inInitialPosition) {
		this.inInitialPosition = inInitialPosition;
	}

	@Override
	public boolean isInInitialPosition() {
		return inInitialPosition;
	}

	@Override
	public List<Move> getLegalMoves(int x, int y, Game game,
			boolean onlyKingThreatening) {
		List<Move> result = new ArrayList<Move>();
		int direction = 1;
		if (Color.BLACK == color) {
			direction = -1;
		}
		// moving
		if (!onlyKingThreatening
				&& game.getBoard().getChessPiece(x + direction, y) == null) {
			if (x + direction == 7 || x + direction == 0) {
				result.add(new Move(x, y, x + direction, y,
						SpecialMoveType.PROMOTING));
			} else {
				result.add(new Move(x, y, x + direction, y));
			}
		}
		if (!onlyKingThreatening && inInitialPosition
				&& game.getBoard().getChessPiece(x + direction, y) == null
				&& game.getBoard().getChessPiece(x + 2 * direction, y) == null) {
			result.add(new Move(x, y, x + 2 * direction, y));
		}
		// attacking
		if (y - 1 >= 0) {
			ChessPiece chessPiece = game.getBoard().getChessPiece(
					x + direction, y - 1);
			if (chessPiece != null && chessPiece.getColor() != color) {
				if (x + direction == 7 || x + direction == 0) {
					result.add(new Move(x, y, x + direction, y - 1, SpecialMoveType.PROMOTING));
				} else {
					result.add(new Move(x, y, x + direction, y - 1));
				}
			}
			// en passant
			if (!onlyKingThreatening && game.getMoves().size() >= 4) {
				addEnPassant(x, y, y - 1, game, result);
			}
		}
		if (y + 1 < 8) {
			ChessPiece chessPiece = game.getBoard().getChessPiece(
					x + direction, y + 1);
			if (chessPiece != null && chessPiece.getColor() != color) {
				if (x + direction == 7 || x + direction == 0) {
					result.add(new Move(x, y, x + direction, y + 1,
							SpecialMoveType.PROMOTING));
				} else {
					result.add(new Move(x, y, x + direction, y + 1));
				}
			}
			if (!onlyKingThreatening && game.getMoves().size() >= 4) {
				addEnPassant(x, y, y + 1, game, result);
			}
		}

		return result;
	}

	public void addEnPassant(int x, int initialY, int y, Game game,
			List<Move> result) {
		if (Color.WHITE == color && x == 4) {
			ChessPiece tmp = game.getBoard().getChessPiece(4, y);
			if (tmp instanceof Pawn) {
				List<Move> gameMoves = game.getMoves();
				Move lastMove = gameMoves.get(gameMoves.size() - 1);
				if (lastMove.getY1() == y && lastMove.getX1() == 6
						&& lastMove.getX2() == 4) {
					result.add(new Move(x, initialY, 5, y,
							SpecialMoveType.EN_PASSANT));
				}
			}
		}
		if (Color.BLACK == color && x == 3) {
			ChessPiece tmp = game.getBoard().getChessPiece(3, y);
			if (tmp instanceof Pawn) {
				List<Move> gameMoves = game.getMoves();
				Move lastMove = gameMoves.get(gameMoves.size() - 1);
				if (lastMove.getY1() == y && lastMove.getX1() == 1
						&& lastMove.getX2() == 3) {
					result.add(new Move(x, initialY, 2, y,
							SpecialMoveType.EN_PASSANT));
				}
			}
		}
	}
	public long getValue() {
		return value;
	}

}
